﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MySql.Data.MySqlClient;

namespace _0812159_0812164.dao
{
    public class CategoryDAO
    {
        public static List<Category> getListCategory()
        {
            List<Category> list = new List<Category>();
            DataHelper helper = new DataHelper();
            helper.open();
            string sql = "SELECT * FROM CATEGORY";
            helper.open();
            MySqlDataReader reader = helper.executeQuery(sql);
            while (reader.Read())
            {
                Category c = new Category();
                c.Idcategory = reader.GetInt32("idcategory");
                c.Namecategory = reader.GetString("namecategory");
                if (!reader.IsDBNull(2))
                    c.Root = reader.GetInt32("root");
                list.Add(c);
            }
            helper.close();

            return list;
        }

        
        public static  List<Category> getListCategoryChild(int nodeID)
        {
            List<Category> listCategory = getListCategory();
            return getListCategoryChild(nodeID, listCategory);
        }

        public static List<Category> getListCategoryChild(int nodeID, List<Category> inListCategory)
        {
            List<Category> lResult = new List<Category>();
            foreach (Category cate in inListCategory)
            {
                if (cate.Root == nodeID)
                    lResult.Add(cate);
            }

            return lResult;            
        }

    }
}
